data = load('ex2data1.txt');
x = data(:,[1,2]);
y = data(:,3);
len = length(y);

pos = find(y == 1);
neg = find(y == 0);
figure;
hold on;
plot(x(pos,1),x(pos,2),'r*');		%以两门考试的成绩分别作为x轴，y轴的坐标建立散点图，此行表示可以考虑的申请者
plot(x(neg,1),x(neg,2),'g*');		%以两门考试的成绩分别作为x轴，y轴的坐标建立散点图，此行表示不予考虑的申请者
xlabel('exam1 scores');
ylabel('exam2 scores');
legend('Ammitted','Not Ammitted');
hold off;

[row, cloumn] = size(x);
new_x = [ones(row, 1) x];
theta = zeros(cloumn + 1,1);
grad = zeros(size(theta));
j = -1 * sum(y .* log(1 ./(1 + exp(-(new_x * theta)))) + (1 - y) .* log((1 - (1 ./ (1 + exp(-(new_x * theta))))))) / len;
grad = (new_x' * (1 ./ (1 + exp(-(new_x * theta))) - y)) / len;
